﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nsf.Samples.RetryActivitySample.Infrastructure.Utils
{
    public static class Extensions
    {
        public static Guid ToGuid(this String source)
        {
            var result = Guid.Empty;
            try
            {
                result = new Guid(source);
            }
            catch (ArgumentNullException)
            {
            }
            catch(FormatException)
            {
            }
            catch(OverflowException)
            {
            }
            return result;
        }

        public static HashSet<TKey> ToHashSet<TKey, TSource>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (keySelector == null)
                throw new ArgumentNullException("keySelector");
            var hashSet = new HashSet<TKey>();
            foreach (var local in source)
                hashSet.Add(keySelector(local));
            return hashSet;
        }

        public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            var hashSet = new HashSet<TSource>();
            foreach (var local in source)
                hashSet.Add(local);
            return hashSet;
        }
    }
}
